package com.sgb.kjwl.viewmodel.home;


import android.widget.Toast;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.RecommendEntity;
import com.sgb.kjwl.model.entity.StoreEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.home.SearchActivity3;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class SearchModel3 extends BaseViewModel {
    private SearchActivity3 activity;
    private HomeRepertory repertory;
    private int pageNum;
    private int pageSize = 10;
    private int total;

    public SearchModel3(SearchActivity3 activity, HomeRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    //商店搜索列表
    public void storeSearch(String text) {
        if (text.equals("")) {
            Toast.makeText(activity.getActivity(), "请输入店铺名称", Toast.LENGTH_SHORT).show();
            return;
        }
        pageNum = 1;
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().storeSearch(repertory.getAreaCode(), text, pageNum, pageSize).enqueue(new Callback<BaseEntity<ListResponse<List<StoreEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<StoreEntity>>>> call, Response<BaseEntity<ListResponse<List<StoreEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    if (response.body().getData().getList() == null)
                        return;
                    total = response.body().getData().getTotal();
                    if (total > 0) {
                        activity.refresh(null, response.body().getData().getList(), response.body().getData().isLastPage());
                    } else {
                        activity.empty();
                    }
                } else {
                    MToast.showToast(activity, response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<StoreEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    //加载商店列表
    public void storeSearchMore(String text) {
        pageNum = pageNum++;
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().storeSearch(repertory.getAreaCode(), text, pageNum, pageSize).enqueue(new Callback<BaseEntity<ListResponse<List<StoreEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<StoreEntity>>>> call, Response<BaseEntity<ListResponse<List<StoreEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    if (response.body().getData().getList() == null)
                        return;
                    activity.loadMore(null, response.body().getData().getList(), response.body().getData().isLastPage());
                } else {
                    MToast.showToast(activity, response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<StoreEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    //商品搜索列表
    public void goodsSearch(String text) {
        if (text.equals("")) {
            Toast.makeText(activity.getActivity(), "请输入商品名称", Toast.LENGTH_SHORT).show();
            return;
        }
        pageNum = 1;
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().goodsSearch(repertory.getAreaCode(), text, pageNum, pageSize).enqueue(new Callback<BaseEntity<ListResponse<List<RecommendEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<RecommendEntity>>>> call, Response<BaseEntity<ListResponse<List<RecommendEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    if (response.body().getData().getList() == null)
                        return;
                    total = response.body().getData().getTotal();
                    if (total > 0) {
                        activity.refresh(response.body().getData().getList(), null, response.body().getData().isLastPage());
                    } else {
                        activity.empty();
                    }
                } else {
                    MToast.showToast(activity, response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<RecommendEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    //加载商品列表
    public void goodsSearchMore(String text) {
        pageNum = pageNum++;
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().goodsSearch(repertory.getAreaCode(), text, pageNum, pageSize).enqueue(new Callback<BaseEntity<ListResponse<List<RecommendEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<RecommendEntity>>>> call, Response<BaseEntity<ListResponse<List<RecommendEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    if (response.body().getData().getList() == null)
                        return;
                    activity.loadMore(response.body().getData().getList(), null, response.body().getData().isLastPage());
                } else {
                    MToast.showToast(activity, response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<RecommendEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }
}
